package com.yuke.cloud.service.pmc.controller.frontend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.pmc.dto.*;
import com.yuke.cloud.service.pmc.entity.ProdProduct;
import com.yuke.cloud.service.pmc.service.ProdProductService;
import com.yuke.cloud.service.pmc.service.ProdSpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* Created by zys on 2018/09/27.
*/
@Slf4j
@Api(value = "WEB - ProdProductController", tags = "处理ProdProduct相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/prod/product")
public class ProdProductController extends BaseController {
    @Resource
    private ProdProductService prodProductService;
    @Resource
    private ProdSpecService prodSpecService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "prodProduct", value = "该数据记录的POJO") @RequestBody ProdProduct prodProduct) {
        prodProductService.save(prodProduct);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        try {
            prodProductService.deleteById(id);
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PostMapping("/ids")
    @ApiOperation(httpMethod = "POST", value = "根据id删除商品规格多条记录")
    public Wrapper deleteByIds(@ApiParam(name = "attributeIds", value = "记录id") @RequestBody ProdIdsDto ids) {
        log.info("ids:"+ids.getIds());
        try {
            prodSpecService.deleteBySpecIds(ids.getIds());
        } catch (DataIntegrityViolationException e) {
            return WrapMapper.error("数据正在使用，无法删除");
        }
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "prodProduct", value = "需要修改的内容") @RequestBody ProdProduct prodProduct) {
        prodProductService.update(prodProduct);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<ProdProduct> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        ProdProduct prodProduct = prodProductService.findById(id);
        return WrapMapper.ok(prodProduct);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdProduct>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<ProdProduct> list = prodProductService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping(value = {"/category/{categoryId}", "/category"})
    @ApiOperation(httpMethod = "GET", value = "商品管理根据分类返回商品列表")
    public Wrapper<PageInfo<ProdProductInfoDto>> getListByCategoryId(@ApiParam(name = "categoryId", value = "商品类别Id") @PathVariable(required = false) Long categoryId,
                                               @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                               @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        if (categoryId == null){
            categoryId = 0L;
        }
        ConditionDto conditionDto = new ConditionDto();
        conditionDto.setCategoryId(categoryId);
        List<ProdProductInfoDto> list = prodProductService.getListsByCondition(conditionDto, page, size);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @PostMapping("/category/condition")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<ProdProductInfoDto>> getListsByCondition(@ApiParam(name = "conditionDto", value = "记录id") @RequestBody(required = false) ConditionDto conditionDto,
                                                                   @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                                   @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {

        List<ProdProductInfoDto> list = prodProductService.getListsByCondition(conditionDto, page, size);
        System.out.println("AAAAAAAAAAAAAAAAAAAA="+list);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @GetMapping("/spec/{productId}")
    @ApiOperation(httpMethod = "GET", value = "商品管理编辑页面")
    public Wrapper<ProdProductDto> getProdByProductId(@ApiParam(name = "productId", value = "商品productId") @PathVariable Long productId) {
        ProdProductDto prodProductDto = prodProductService.getProdByProductId(productId);
        return WrapMapper.ok(prodProductDto);
    }

    @PutMapping("/spec")
    @ApiOperation(httpMethod = "PUT", value = "商品管理修改一条记录")
    public Wrapper updateBySpecId(@ApiParam(name = "prodProduct", value = "需要修改的商品基本信息") @RequestBody ProdProductDto prodProductDto) {
        List<ProdSpecDto> saveProdSpecDtos = prodProductDto.getProdSpecDtos().stream().filter(prodSpecDto -> prodSpecDto.getProdSpec().getSpecId()==null).collect(Collectors.toList());
        if (saveProdSpecDtos.size()>0){
            prodProductService.saveUpdateInfo(prodProductDto.getProdProduct(), saveProdSpecDtos);
        }
        List<ProdSpecDto> updateProdSpecDtos = prodProductDto.getProdSpecDtos().stream().filter(prodSpecDto -> prodSpecDto.getProdSpec().getSpecId()!=null).collect(Collectors.toList());

        prodProductService.updateBySpecId(prodProductDto.getProdProduct(), updateProdSpecDtos);
//        for (ProdSpecDto updateProdSpecDto : updateProdSpecDtos) {
//            prodProductService.updateBySpecId(prodProductDto.getProdProduct(), updateProdSpecDto);
//        }
        return WrapMapper.ok();
    }

    @PutMapping("/isEnable/{specId}/{isEnable}")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录(是否启用)")
    public Wrapper isEnable(@ApiParam(name = "specId", value = "specId") @PathVariable Long specId,
                            @ApiParam(name = "isEnable", value = "isEnable") @PathVariable Integer isEnable) {
        prodSpecService.isEnable(specId, isEnable);
        return WrapMapper.ok();
    }

    @PutMapping("/isEnableProduct/{productId}/{specId}/{isEnable}")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录(是否启用)")
    public Wrapper isEnableProduct(@ApiParam(name = "productId", value = "productId") @PathVariable Long productId,
                                   @ApiParam(name = "specId", value = "specId") @PathVariable Long specId,
                            @ApiParam(name = "isEnable", value = "isEnable") @PathVariable Integer isEnable) {
        prodSpecService.isEnableProduct(productId, specId,isEnable);
        return WrapMapper.ok();
    }



    @PostMapping("/addAll")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper addAllInfo(@ApiParam(name = "prodProductDto", value = "该商品所有数据记录的POJO") @RequestBody ProdProductDto prodProductDto) {

        if (prodProductDto == null) {
            return  WrapMapper.error("prodProductDto is null");
        }
        ProdProduct prodProduct = prodProductDto.getProdProduct();
        List<ProdSpecDto> prodSpecDtos = prodProductDto.getProdSpecDtos();
        if (prodProduct == null || prodSpecDtos == null) {
            return  WrapMapper.error("prodProduct || prodSpecDtos is null");
        }
        prodProductService.saveAllInfo(prodProduct, prodSpecDtos);
        return WrapMapper.ok();
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据ID获取具体信息")
    public Wrapper ifExist(@ApiParam(name = "id", value = "记录id") @RequestBody ProdProduct prodProduct) {
        int count = prodProductService.selectCount(prodProduct);
        return WrapMapper.ok(count);
    }

    @PostMapping("/list")
    @ApiOperation(httpMethod = "POST", value = "根据条件获取商品")
    public Wrapper<List<ProdProduct>> getListByCondition(@ApiParam(name = "prodProduct", value = "该数据记录的POJO") @RequestBody ProdProduct prodProduct) {
        return WrapMapper.ok(prodProductService.getListByCondition(prodProduct));
    }

    //根据商品的productId,删除商品
    @PostMapping("/deleteByProductId")
    @ApiOperation(httpMethod = "POST", value = "根据productId删除商品")
    public Wrapper deleteByProductId(@ApiParam(name = "productId", value = "商品id")  @RequestBody ProdProduct prodProduct ){
        prodProductService.deleteByProductId(prodProduct.getProductId());
        return WrapMapper.ok();
     }
}

